<?php
// @formatter:off
/**
 * @file dirs.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-07 02:16:25 +0000 (Fri, 07 Jun 2013) $
 *
 * $Id: dirs.php 71 2013-06-07 02:16:25Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/extensions/TopFive/dirs.php $
 */
// @formatter:on

function TopFive_dirs_linkChecker() {
	global $wcAction;
	//
	// Only index, mobile and others are not allowed.
	$ok = defined("__INDEX__") && in_array($wcAction, array(
		WC_ACTION_DIRECTORY_LIST,
		WC_ACTION_DIRECTORY_SHOW
	));
	//
	// Ok, everything ok.
	if($ok) {
		global $wcDatabaseAccess;
		global $wcGroup;
		global $wcThemeAssigns;

		$db = WCConnection::Instance();
		$dbprefix = $wcDatabaseAccess['prefix'];

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "select  dir_id,\n";
			$query .= "        dir_name,\n";
			$query .= "        dir_count\n";
			$query .= "from    {$dbprefix}directories\n";
			$query .= "where   dir_group = :group\n";
			$query .= "order by dir_count desc\n";
			$query .= "limit 5\n";
			$stmt = $db->prepare($stmtId, $query);
		}

		$list = array();
		if($stmt->execute(array(":group" => $wcGroup->id))) {
			foreach($stmt->fetchAll() as $row) {
				$list[] = array(
					"id" => $row["dir_id"],
					"name" => $row["dir_name"],
					"count" => $row["dir_count"]
				);
			}
		}
		$wcThemeAssigns["TopFive_dirs"] = $list;

		$ok = true;
	}

	return $ok;
}
?>